﻿using System;
using System.Text;

namespace iTPS.Service.Core.Cryptography
{
    public static class HexExtension
    {
        public static string ToHexString(this byte[] bytes) {
            int length = bytes.Length;
            var builder = new StringBuilder(length * 2);
            for (int i = 0; i < length; i++)
            {
                builder.Append("0123456789ABCDEF"[(bytes[i] & 240) >> 4]);
                builder.Append("0123456789ABCDEF"[bytes[i] & 15]);
            }
            return builder.ToString();
        }

        public static byte[] ToByte(this string hexString) {
            hexString = hexString.TrimEnd("\r\n".ToCharArray());
            byte[] returnBytes;
            returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }
            return returnBytes;
        }

    }
}
